package com.example.demo.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/resp")
public class ResponseController {
    @RequestMapping("/res1")
    public String res1(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/res2")
    public String res2(){
        return "返回数据";
    }

    @ResponseBody
    @RequestMapping("/res3")
    public String res3(){
        return "<h1>hello, html</h1>";
    }

    @ResponseBody
    @RequestMapping("/res4")
    public Map<String, String> res4(){
        Map<String, String> map = new HashMap<>();
        map.put("key1", "val1");
        map.put("key2", "val2");
        map.put("key3", "val3");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(404);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping(value = "/returnJson", produces = "application/json")
    public String returnJson(){
        return "{\"id\":1,\"name\":\"zhangsan\"}";
    }
}
